home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / me_cd25.zip / MUTT2.ZIP / AUTOLOAD.MUT < prev    next >
Lisp/Scheme  |  1992-11-09  |  5KB  |  97 lines

  1.   ;; autoload.mut : Delayed file loading.
  2.   ;; Autoloading files means files are not loaded until they are used.  This
  3.   ;;   can result in smaller init files and faster startup times.
  4.   ;;
  5.   ;; How to use:
  6.   ;;   (autoload function-name file-name)
  7.   ;;     When function-name is called, file-name is loaded and a call is
  8.   ;;       again made to function-name.  file-name MUST have redefined
  9.   ;;       function-name (or you will find your self in a infinite loop).
  10.   ;;   (autoload-with-keypress file-name)
  11.   ;;     Same as autoload but the key that caused the autoload is pressed
  12.   ;;       again.
  13.   ;;     file-name MUST rebind the key (the usual case) or contain a
  14.   ;;       function with the same name as the function the key was bound
  15.   ;;       to.  If not, you will load forever.
  16.   ;;     You would use this function (instead of autoload) when you want
  17.   ;;       (more than one key) to autoload a file and don't want to autoload
  18.   ;;       each function.  This can save some code because lots of keys can
  19.   ;;       be bound to a single autoload function.
  20.   ;;
  21.   ;; Notes:
  22.   ;;   The function that calls autoload MUST be late bound else autoload
  23.   ;;     will be called every time the function is called.  bind-to-key is
  24.   ;;     always later bound.  For function calls use:
  25.   ;;     (floc "function-to-be-autoloaded" args) or
  26.   ;;     (floc "function-to-be-autoloaded" ()) if no args.
  27.   ;;   Autoloaded functions can take arguments - the args after fcn-name and
  28.   ;;     file-name are passed to the autoloaded function (if they are
  29.   ;;     passed to autoload:  use (push-args) or push them explictly).
  30.   ;;
  31.   ;; Examples:
  32.   ;;   (defun foo { (floc "bar" 1 "two" 3) })
  33.   ;;   (defun bar { (autoload "bar" "bar.mco" (push-args 0)) })
  34.   ;;     Whenever foo or bar is called, bar.mco will loaded and bar called.
  35.   ;;       If foo is called, bar is called with the arg list:  1 "two" 3.
  36.   ;;     The next time foo is called, bar will be called directly:  the
  37.   ;;       autoloading function will be by-passed.
  38.   ;;   (defun bar { (autoload-with-keypress "bar.mut") })
  39.   ;;   (bind-to-key bar "1")
  40.   ;;   (bind-to-key bar "2")
  41.   ;;     When 1 or 2 is pressed, bar.mco is loaded and the key is pressed
  42.   ;;       again.  bar.mut MUST rebind 1 AND 2 in a MAIN or contain a
  43.   ;;       function named bar (not very useful because that would mean that
  44.   ;;       both 1 and 2 always run bar).
  45.   ;;   
  46.   ;; How autoload works:
  47.   ;;   File one.mut contains:
  48.   ;;     (defun two  { (autoload "two" "two.mut") }
  49.   ;;     (defun MAIN { (two) })
  50.   ;;   File two.mut contains:
  51.   ;;     (defun two { (msg "This is 2") })
  52.   ;;   When you load one.mco, MAIN is run and the function two is called.
  53.   ;;     two calls autoload and autoload loads two.mco.  Here is where the
  54.   ;;     magic happens:  When two.mco is loaded, the new function two
  55.   ;;     overwrites the old two in the (function-name, function-address)
  56.   ;;     table (the code for both is still loaded).  By using late binding
  57.   ;;     (the (floc)), "two" is searched for in the table and the address
  58.   ;;     of the new two is found and that function is run.  The sequence
  59.   ;;     (from the start) is:  call two, load two.mco, overwrite the old
  60.   ;;     two in the table, search the table for "two", find the new two and
  61.   ;;     run it.  An important thing to remember is the compiler can 
  62.   ;;     use early (static) or late binding.  If you don't tell it to use
  63.   ;;     late binding, it uses early (if it can).  Thats why you need to be
  64.   ;;     sure and use late binding:  so all this magic works.  If a
  65.   ;;     function is static bound to autoload, autoload will be called
  66.   ;;     every time the first function is called, which is not what you
  67.   ;;     want.  ME key binding is always late bound.
  68.   ;;   The reason key bindings don't go away when autoloading is because a
  69.   ;;     file other than the one the keys are originally bound in is
  70.   ;;     loaded.  If one.mut had also contained:
  71.   ;;       (bind-to-key "two" "1")
  72.   ;;     (and no MAIN) then:  when 1 is pressed, two.mco is loaded.  The
  73.   ;;     new function two overwrites the old two but the key is not
  74.   ;;     collected (since it didn't point to a function in two.mco) and now
  75.   ;;     points to the new two.  Pressing 1 will run the new two.  If you
  76.   ;;     load two.mco again, all keys that point to functions in two.mco
  77.   ;;     are garbage collected and you would lose the key.
  78.   ;; 
  79.   ;; C Durland    Public Domain
  80.  
  81. (defun
  82.   autoload (string fcn-name file-name) (optional-args) HIDDEN
  83.   {
  84.     (if (load file-name)
  85.       (floc (fcn-name) (push-args 2))
  86.       (msg "autoload: Could not load " file-name)
  87.     )
  88.   }
  89.   autoload-with-keypress (string file-name) HIDDEN
  90.   {
  91.     (if (load file-name)
  92.       (exe-key (key-pressed))
  93.       (msg "autoload: Could not load " file-name)
  94.     )
  95.   }
  96. )
  97.